// mongoose setup
require( '../../../../config' );
var Promise  = require('promise'),
    assert = require('assert'),
    fs = require('fs'),
    helper = require('../../../../helper'),
    sinon   = require('sinon'),  sinonSandbox,
    weixinDao = require( '../../../../../app/components/backend/db').weixinuser,
    weixinModule = require( '../../../../../app/components/backend/db/models/weixinuser'),
    testData = JSON.parse(fs.readFileSync('test/components/backend/db/dao/user_data.json', 'utf8'));

describe('app/components/backend/db/daos/weixinuser', function() {
  // 测试后置处理
  afterEach(function() {
    if (sinonSandbox) sinonSandbox.restore();
    // 清空weixinusers collection
    weixinModule.remove({}, function(err) {
      if (err) throw new Error(err);
      console.log('\n>>>>>>>>>> collection removed');
    });
  });

  describe('#logicDeleteByOpenId()', function() {
    it('[logicDeleteByOpenId.Nor.01].逻辑删除一个用户', function() {
      return weixinDao.upsert({openid: testData.openid}, testData).
        then(weixinDao.logicDeleteByOpenId(testData.openid)).
        then(function(data) {
          console.log(data);
        }).then(weixinDao.deleteByOpenId(testData.openid));
    });
  });

  describe('#upsert()', function() {
    it('[weixinuser.upsert.Nor.01].upsert数据', function() {
      var upsertPromise = weixinDao.upsert({openid: testData.openid}, testData);
      helper.isBluebirdPromise(upsertPromise);
      return upsertPromise.then(function (doc) {
        assert.equal(doc.ok, 1);
      });
    });

    it('[weixinuser.upsert.Error.01].upsert出错', function() {
      var upsertPromise = weixinDao.upsert({openid: testData.openid}, testData);
      helper.isBluebirdPromise(upsertPromise);
      sinonSandbox = sinon.sandbox.create();
      sinonSandbox.stub(require('mongoose').Query.prototype, 'constructor').throws("ERROR");
      return upsertPromise.then(function (doc) {
        assert.equal(doc.ok, 1);
      }).catch(function (err) {
        console.log(err);
      });
    });
  });

  describe('#findOneByEmail()', function() {
    it('[findOneByEmail.Nor.01].通过email查找用户', function() {
      testData.email = 'neil.zeng@tsinghua.edu.cn';

      // 插入记录
      return weixinDao.upsert({openid: testData.openid}, testData).
        then(function() {

          // 查找记录
          return weixinDao.findOneByEmail(testData.email);
        }).then(function (data) {

          // 结果判定
          if (data) assert.ok(true);
          assert.equal(testData.email, data.email);
          return Promise.resolve();
        }).catch(function(err) {

          // 错误则抛出
          return Promise.reject(err);
        });
    });
  });
  describe('#findOneAndUpdate()', function() {
    it('[findOneAndUpdate.Nor.01].按照条件查找, 有:更新, 无:插入', function() {
      var findPromise = weixinDao.findOneAndUpdate({"openid": 'oNaHXsspWthiBKBda-E5C_OBP_ao'}, testData);
      helper.isBluebirdPromise(findPromise);
      return findPromise.then(function (doc) {
          console.log(doc);
          return Promise.resolve();
        });
    });
    it('[findOneAndUpdate.Nor.02].按照条件查找, 有:更新, 无:插入, 并执行回调函数', function(done) {
      weixinDao.findOneAndUpdate({"openid": 'oNaHXsspWthiBKBda-E5C_OBP_ao'}, testData, function (err, doc) {
        assert.equal(doc.openid, 'oNaHXsspWthiBKBda-E5C_OBP_ao');
        done();
      });
    });
  });

  describe('#findOne()', function() {
    beforeEach(function() {
      return weixinDao.findOneAndUpdate(testData, testData);
    });
    it('[findOne.Nor.01].按照条件查找, 有:更新, 无:插入', function() {
      return weixinDao.findOne({openid: testData.openid}).then(function(userInfo) {
          assert.equal(testData.openid, userInfo.openid);
          assert.equal(0, userInfo.point); // 默认值
          return Promise.resolve();
        });
    });
  });
});